﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class SliderJoint : Joint
    {
        private FarseerGames.FarseerPhysics.Dynamics.Joints.SliderJoint mSliderJoint;
        private RigidBody mConnectedBody = null;

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Joint is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the first RigidBody.
        /// </summary>
        public Vector2 Anchor1 { get { return mSliderJoint.Anchor1; } set { mSliderJoint.Anchor1 = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the second RigidBody.
        /// </summary>
        public Vector2 Anchor2 { get { return mSliderJoint.Anchor2; } set { mSliderJoint.Anchor2 = value; } }

        /// <summary>
        /// Gets the world anchor point of the first RigidBody.
        /// </summary>
        public Vector2 WorldAnchor1 { get { return mSliderJoint.WorldAnchor1; } }

        /// <summary>
        /// Gets the world anchor point of the second RigidBody.
        /// </summary>
        public Vector2 WorldAnchor2 { get { return mSliderJoint.WorldAnchor2; } }

        /// <summary>
        /// Gets or sets the minimum distance the anchors can come together.
        /// </summary>
        public Single MinDistance { get { return mSliderJoint.Min; } set { mSliderJoint.Min = value; } }

        /// <summary>
        /// Gets or sets the maximum distance the anchors can be seperate from each other.
        /// </summary>
        public Single MaxDistance { get { return mSliderJoint.Max; } set { mSliderJoint.Max = value; } }

        /// <summary>
        ///  Gets or sets the slop allowed past the Min/Max limits.
        /// </summary>
        public Single Slop { get { return mSliderJoint.Slop; } set { mSliderJoint.Slop = value; } }

        /// <summary>
        /// Gets the current anchor position of the joint.
        /// </summary>
        public Vector2 CurrentAnchorPosition { get { return mSliderJoint.CurrentAnchorPosition; } }

        /// <summary>
        /// Creates an instance of a Slider component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public SliderJoint(GameObject gameObject)
            : base(gameObject)
        {            
            mSliderJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.SliderJoint();
            mJoint = mSliderJoint;            
        }

        /// <summary>
        /// Create the Joint after setting initial settings such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {
            mSliderJoint = JointFactory.Instance.CreateSliderJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, GameObject.RigidBody.Body.Position, ConnectedBody.Body, ConnectedBody.Body.Position, MinDistance, MaxDistance);
            mJoint = mSliderJoint;
        }
    }
}
